﻿using System;
using System.Collections.Generic;
using System.Text;
using FlatRedBall;
using FlatRedBall.Input;
using FlatRedBall.AI.Pathfinding;
using FlatRedBall.Graphics.Animation;
using FlatRedBall.Graphics.Particle;

using FlatRedBall.Graphics.Model;
using FlatRedBall.Math.Geometry;
using FlatRedBall.Math.Splines;

using Cursor = FlatRedBall.Gui.Cursor;
using GuiManager = FlatRedBall.Gui.GuiManager;
using FlatRedBall.Localization;

#if FRB_XNA || SILVERLIGHT
using Keys = Microsoft.Xna.Framework.Input.Keys;
using Vector3 = Microsoft.Xna.Framework.Vector3;
using Texture2D = Microsoft.Xna.Framework.Graphics.Texture2D;
#endif

using NereusGame.Entities.Ships.Enemies;
using NereusGame.Entities.Ships;
using NereusGame.Entities.HBodies;
using NereusGame.Entities.Projectiles;
using FlatRedBall.Instructions;

namespace NereusGame.Screens
{
   public class PauseScreen
    {
        protected const string CONTEN_MANAGER = "NereusFinalContent";   // Name of the contentManager
        private List<Sprite> buttonsN;      // create list of normal buttons
        private List<Sprite> buttonsH;      // create list of selected butons
        private const int MAX_BUTTONS = 3;  // maximum number of buttons
        public int selector;                // indicates which button is selected
        public Xbox360GamePad gamepad;
        private Camera camera;
        private bool active;
        // constructor
        public PauseScreen(Camera mCamera,Xbox360GamePad gamepad1)
        {
            camera = mCamera;
            gamepad = gamepad1;             // assign gamepad
            buttonsN = new List<Sprite>();
            buttonsH = new List<Sprite>();
            selector = 0;
            active = false;

            // Initialize all the buttons
            for (int i = 0; i < MAX_BUTTONS; i++)
            {
                buttonsN.Add(new Sprite());
                SpriteManager.AddSprite(buttonsN[i]);
                SpriteManager.AddToLayer(buttonsN[i], camera.Layer);
                buttonsH.Add(new Sprite());
                SpriteManager.AddSprite(buttonsH[i]);
                SpriteManager.AddToLayer(buttonsH[i], camera.Layer);
                buttonsH[i].ScaleY = buttonsN[i].ScaleY = 1.6f;
                buttonsH[i].ScaleX = buttonsN[i].ScaleX = buttonsN[i].ScaleY * 5f;
                buttonsH[i].Visible = buttonsN[i].Visible = false;
            }
            // assign textures
          //  buttonsN[0].Texture = FlatRedBallServices.Load<Texture2D>(@"content/Screens/PauseScreen/Resume", CONTEN_MANAGER);
           // buttonsN[1].Texture = FlatRedBallServices.Load<Texture2D>(@"content/Screens/PauseScreen/Restart", CONTEN_MANAGER);
          //  buttonsN[2].Texture = FlatRedBallServices.Load<Texture2D>(@"content/Screens/PauseScreen/ExitToMainMenu", CONTEN_MANAGER);
         //   buttonsH[0].Texture = FlatRedBallServices.Load<Texture2D>(@"content/Screens/PauseScreen/ResumeH", CONTEN_MANAGER);
        //    buttonsH[1].Texture = FlatRedBallServices.Load<Texture2D>(@"content/Screens/PauseScreen/RestartH", CONTEN_MANAGER);
          //  buttonsH[2].Texture = FlatRedBallServices.Load<Texture2D>(@"content/Screens/PauseScreen/ExitToMainMenuH", CONTEN_MANAGER); 
        }

        // Show all the normal buttons
        public void ShowButtons()
        {
            if (InstructionManager.IsEnginePaused && active == false)
            {
                Vector3 pos = camera.Position;
                pos.Z = 5;
                for (int i = 0; i < MAX_BUTTONS; i++)
                {
                    buttonsN[i].Position = pos;
                    buttonsH[i].Position = pos;
                    pos.Y -= buttonsN[i].ScaleY * 3f;
                    buttonsN[i].Visible = true;
                }
                selector = 0;
                active = true;
            }

        }

        // hide all the buttons
        public void HideButtons()
        {
            if (!InstructionManager.IsEnginePaused && active == true)
            {
                for (int i = 0; i < MAX_BUTTONS; i++)
                {
                    buttonsN[i].Visible = false;
                    buttonsH[i].Visible = false;
                }
                active = false;
            }
        }


        // update the paused screen
        public void Update()
        {
            Select();
            MoveSelector();
            ShowButtons();
            HideButtons();
        }

        // navigate through buttons
        private void MoveSelector()
        {
            if (InstructionManager.IsEnginePaused)
            {
                // move down
                if (gamepad.ButtonPushed(Xbox360GamePad.Button.DPadDown) || gamepad.LeftStick.AsDPadPushed(Xbox360GamePad.DPadDirection.Down))
                {
                    buttonsN[selector].Visible = true;
                    buttonsH[selector].Visible = false;
                    selector++;
                    if (selector > MAX_BUTTONS - 1)
                        selector = 0;
                }

                // move up
                else if (gamepad.ButtonPushed(Xbox360GamePad.Button.DPadUp) || gamepad.LeftStick.AsDPadPushed(Xbox360GamePad.DPadDirection.Up))
                {
                    buttonsN[selector].Visible = true;
                    buttonsH[selector].Visible = false;
                    selector--;
                    if (selector < 0)
                        selector = MAX_BUTTONS - 1;
                }

                // select button
                buttonsN[selector].Visible = false;
                buttonsH[selector].Visible = true;
            }

        }

        // remove sprites
        public void Destroy()
        {

            for (int i = 0; i < MAX_BUTTONS; i++)
            {
                SpriteManager.RemoveSprite(buttonsN[i]);
                SpriteManager.RemoveSprite(buttonsH[i]);
            }
        }



        // toogle pause screen
        private void Select()
        {/*
            if (!InstructionManager.IsEnginePaused)
            {
                // pause the game and bring up the pauseMenu
                if (gamepad.ButtonReleased(Xbox360GamePad.Button.Start) || gamepad.ButtonReleased(Xbox360GamePad.Button.Back))
                {
                    InstructionManager.PauseEngine();
                }
            }
            else
            {
                // Cancel the pause screen and navigate back
                if (gamepad.ButtonReleased(Xbox360GamePad.Button.B))
                {
                    InstructionManager.UnpauseEngine();
                }
                // select an option in the pause menu
                else if (gamepad.ButtonReleased(Xbox360GamePad.Button.A))
                {
                    
                    // Restart level
                    if (selector == 1)
                    {

                        HideButtons();
                        InstructionManager.UnpauseEngine();
                        if (Game1.mGameMode == Game1.GameMode.Arena)
                        {
                            SpriteManager.Camera.SetSplitScreenViewport(Camera.SplitScreenViewport.FullScreen);
                        }
                        ScreenManager.CurrentScreen.MoveToScreen(typeof(LoadScreen).FullName);
                    }
                    // Resume level
                    else if (selector == 0)
                    {
                        HideButtons();
                        InstructionManager.UnpauseEngine();
                    }
                    // Exit to main menu
                    else if (selector == 2)
                    {
                        HideButtons();
                        InstructionManager.UnpauseEngine();
                        if (Game1.mGameMode == Game1.GameMode.Arena)
                        {
                            SpriteManager.Camera.SetSplitScreenViewport(Camera.SplitScreenViewport.FullScreen);
                        }
                        ScreenManager.CurrentScreen.MoveToScreen(typeof(Menu).FullName);
                    }

                }
            }

        */}

        public void SetGamePad(Xbox360GamePad mgamePad)
        {
            gamepad = mgamePad;
        }

        public void SetCamera(Camera c)
        {
            for (int i = 0; i < buttonsN.Count; i++)
            {
                camera.Layer.Remove(buttonsN[i]);
                camera.Layer.Remove(buttonsH[i]);
            }

            camera = c;
            for (int i = 0; i < buttonsN.Count; i++)
            {

                SpriteManager.AddToLayer(buttonsN[i], camera.Layer);
                SpriteManager.AddToLayer(buttonsH[i], camera.Layer);
            }
        }



    }
}
